package cn.ffcs.bpm.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionContext;

import cn.ffcs.base.web.BaseAction;
import cn.ffcs.bpm.model.DocumentType;
import cn.ffcs.bpm.model.NextTep;
import cn.ffcs.bpm.model.ProcessConfig;
import cn.ffcs.bpm.model.ProcessConfigDetail;
import cn.ffcs.bpm.model.ProcessStep;
import cn.ffcs.bpm.model.TransactorType;
import cn.ffcs.bpm.service.DocumentTypeService;
import cn.ffcs.bpm.service.NextTepService;
import cn.ffcs.bpm.service.ProcessConfigDetailService;
import cn.ffcs.bpm.service.ProcessConfigService;
import cn.ffcs.bpm.service.ProcessStepService;
import cn.ffcs.bpm.service.TransactorTypeService;
import cn.ffcs.product.model.ProductReference;
import cn.ffcs.system.dict.model.Dict;
import cn.ffcs.system.dict.model.DictType;
import cn.ffcs.system.dict.service.DictService;
import cn.ffcs.system.dict.service.DictTypeService;
import cn.ffcs.util.MyList;
import cn.ffcs.util.context.ApplicationContextUtil;

/**
 * 流程节点配置action层，实现页面业务逻辑<br/>
 * <table>
 * <tr>
 * <td>负责人/principal:</td>
 * <td colspan="2">pany</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author pany
 * @version $Id$
 */
public class ProcessConfigDetailAction<P extends ProcessConfigDetail> extends
        BaseAction<P>
{
    private static final int BUFFER_SIZE = 512 * 1024;
    /** 业务POJO **/
    private P processConfigDetail;
    /** 流程节点服务 **/
    private ProcessConfigDetailService processConfigDetailService;
    /** 流程步骤服务 **/
    private ProcessStepService processStepService;
    /** 流程配置服务 **/
    private ProcessConfigService processConfigService;
    /** 办理人类型服务 **/
    private TransactorTypeService transactorTypeService;
    /** 办理人类型服务 **/
    private NextTepService nextTepService;
    /** 文档类型服务 **/
    private DocumentTypeService documentTypeService;
    /** 数据字典定义类型服务 **/
    private DictTypeService dictTypeService;
    /** 数据字典定义服务 **/
    private DictService dictService;
    /** 流程配置数据集 **/
    private List<ProcessConfig> processConfigs;
    /** 流程步骤数据集 **/
    private List<ProcessStep> processSteps;
    /** 办理人类型数据集 **/
    private List<TransactorType> transactorTypes;
    /** 下一节点数据集 **/
    private List<NextTep> nextTeps;
    /** 流程步骤数据集 **/
    private List<P> processConfigDetails;
    /** 文档类型数据集 **/
    private List<DocumentType> documentTypes;
    /** 所有定义的文档资料类型数据集 **/
    private List<Dict> lRefType;
    

    public String init()
    {
        // 获取流程配置、流程步骤、办理人类型数据集
        processSteps = processStepService.findAll();
        processConfigs = processConfigService.findAll();
        transactorTypes = transactorTypeService.findAll();
        processConfigDetails = baseService.findAll();
        
        //取文档资料类型
        String dtKey = new ProductReference().getClass().getSimpleName();
        Criterion condDt = Restrictions.eq("keyValue", dtKey.toUpperCase());
        List<DictType> ldt = dictTypeService.findBy(condDt);
        if (ldt != null && ldt.size() > 0)
        {
            DictType dictType = (DictType) ldt.get(0);
            Criterion cond = Restrictions.eq("dictType.id", dictType.getId());
            lRefType = dictService.findBy(cond);
        }
        else
        {
            lRefType = new ArrayList<Dict>();
        }
        
        if (processConfigDetail != null && processConfigDetail.getId() != 0)
        {
            Criterion cond = null;
            cond = Restrictions.eq("processDetail", processConfigDetail);
            //获得已经设置的下一节点
            nextTeps = nextTepService.findBy(cond);
            Criterion cond1 = null;
            cond1 = Restrictions.eq("processDetail", processConfigDetail);
            documentTypes=documentTypeService.findBy(cond1);
        }
        return SUCCESS;
    }

    /**
     * 根据关键字获取实体数据
     * @return
     */
    public String findById()
    {
        if (processConfigDetail != null && processConfigDetail.getId() != 0)
        {
            processConfigDetail = (P) baseService.findById(processConfigDetail.getId());
        }
        init();
        return SUCCESS;
    }

    /**
     * 删除操作
     * @return
     */
    public String remove()
    {
        baseService.remove(processConfigDetail);
        return SUCCESS;
    }

    /**
     * 新增或修改产品产品字典数据
     * @return
     */
    public String saveOrUpdate()
    {
        processConfigDetailService.saveOrUpdate(processConfigDetail);

        returnMsg = "保存操作已成功！";
        init();
        return SUCCESS;
    }
    
    /**
     * 新增或修改产品产品字典数据
     * @return
     */
    public String findDocTypeByDetailId(){
        if (super.getConds() != null)
        {
            String processDetail = super.getConds().get("processDetail") + "";
            if (!"".equals(processDetail))
            {
                Criterion cond = Restrictions.eq("processDetail.id", new Integer(processDetail));
                List<DocumentType> list = documentTypeService.findBy(cond);
                for(int i=0;i<list.size();i++){
                    DocumentType document=list.get(i);
                    Dict dict=document.getDocType();
                    dict.getId();
                    dict.getName();
                    
                }
            }
        }
        ActionContext ctx = ActionContext.getContext();
        HttpServletResponse response = (HttpServletResponse) ctx
                .get(ServletActionContext.HTTP_RESPONSE);
        String xml = MyList.getAll(documentTypes);
        response.setCharacterEncoding("gb2312");// 页面编码设置
        response.setContentType("text/xml");// 页面文件头格式
        System.out.println(xml);
        try
        {
            response.getWriter().write(xml);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }
    
    public String findByPage(){
    	String processConfigId=ApplicationContextUtil.getContextObject("processConfigId");
    	if(StringUtils.isNotBlank(processConfigId)){
    		getConds().put("processConfig.id", Integer.valueOf(processConfigId));
    	}
    	List processList=processConfigService.findAll();
    	ApplicationContextUtil.getHttpServletRequest().setAttribute("processList", processList);
    	return super.findByPage();
    }
    public P getProcessConfigDetail()
    {
        return processConfigDetail;
    }

    public void setProcessConfigDetail(P processConfigDetail)
    {
        this.processConfigDetail = processConfigDetail;
    }

    public List<ProcessConfig> getProcessConfigs()
    {
        return processConfigs;
    }

    public void setProcessConfigs(List<ProcessConfig> processConfigs)
    {
        this.processConfigs = processConfigs;
    }

    public ProcessConfigService getProcessConfigService()
    {
        return processConfigService;
    }

    public void setProcessConfigService(
            ProcessConfigService processConfigService)
    {
        this.processConfigService = processConfigService;
    }

    public List<ProcessStep> getProcessSteps()
    {
        return processSteps;
    }

    public void setProcessSteps(List<ProcessStep> processSteps)
    {
        this.processSteps = processSteps;
    }

    public ProcessStepService getProcessStepService()
    {
        return processStepService;
    }

    public void setProcessStepService(ProcessStepService processStepService)
    {
        this.processStepService = processStepService;
    }

    public List<TransactorType> getTransactorTypes()
    {
        return transactorTypes;
    }

    public void setTransactorTypes(List<TransactorType> transactorTypes)
    {
        this.transactorTypes = transactorTypes;
    }

    public TransactorTypeService getTransactorTypeService()
    {
        return transactorTypeService;
    }

    public void setTransactorTypeService(
            TransactorTypeService transactorTypeService)
    {
        this.transactorTypeService = transactorTypeService;
    }

    public List<P> getProcessConfigDetails()
    {
        return processConfigDetails;
    }

    public void setProcessConfigDetails(List<P> processConfigDetails)
    {
        this.processConfigDetails = processConfigDetails;
    }

    public NextTepService getNextTepService()
    {
        return nextTepService;
    }

    public void setNextTepService(NextTepService nextTepService)
    {
        this.nextTepService = nextTepService;
    }

    public ProcessConfigDetailService getProcessConfigDetailService()
    {
        return processConfigDetailService;
    }

    public void setProcessConfigDetailService(
            ProcessConfigDetailService processConfigDetailService)
    {
        this.processConfigDetailService = processConfigDetailService;
    }

    public List<NextTep> getNextTeps()
    {
        return nextTeps;
    }

    public void setNextTeps(List<NextTep> nextTeps)
    {
        this.nextTeps = nextTeps;
    }

    public List<DocumentType> getDocumentTypes()
    {
        return documentTypes;
    }

    public void setDocumentTypes(List<DocumentType> documentTypes)
    {
        this.documentTypes = documentTypes;
    }

    public DocumentTypeService getDocumentTypeService()
    {
        return documentTypeService;
    }

    public void setDocumentTypeService(DocumentTypeService documentTypeService)
    {
        this.documentTypeService = documentTypeService;
    }

    public DictService getDictService()
    {
        return dictService;
    }

    public void setDictService(DictService dictService)
    {
        this.dictService = dictService;
    }

    public DictTypeService getDictTypeService()
    {
        return dictTypeService;
    }

    public void setDictTypeService(DictTypeService dictTypeService)
    {
        this.dictTypeService = dictTypeService;
    }

    public List<Dict> getLRefType()
    {
        return lRefType;
    }

    public void setLRefType(List<Dict> refType)
    {
        lRefType = refType;
    }

}
